package com.restaurant.application.root.commonui.data;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;
import java.util.stream.Collectors;

public class EditorPage implements PickableBean {

    public static final String FN_NAME = "name";
    public static final String FN_SAVEPATH = "savePath";
    public static final String FN_CANCELPATH = "cancelPath";
    public static final String FN_FIELDS = "fields";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_NAME,
            FN_SAVEPATH,
            FN_CANCELPATH,
            FN_FIELDS);

    private String name;
    private String savePath;
    private String cancelPath;
    private List<EditorValue> fields;

    public EditorPage(String name, String savePath, String cancelPath, List<EditorValue> fields) {
        setName(name);
        setSavePath(savePath);
        setCancelPath(cancelPath);
        setFields(fields);
    }

    public EditorPage() {
    }

    public EditorPage duplicate() {
        EditorPage result = new EditorPage();
        result.name = this.name;
        result.savePath = this.savePath;
        result.cancelPath = this.cancelPath;
        result.fields = this.fields == null ? null : this.fields.stream().map(e1 -> e1 == null ? null : e1.duplicate()).collect(Collectors.toList());
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

    public EditorPage withName(String value) {
        setName(value);
        return this;
    }

    public EditorPage withName(String value, String defaultValue) {
        setName(value == null ? defaultValue : value);
        return this;
    }

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String value) {
        this.savePath = value;
    }

    public EditorPage withSavePath(String value) {
        setSavePath(value);
        return this;
    }

    public EditorPage withSavePath(String value, String defaultValue) {
        setSavePath(value == null ? defaultValue : value);
        return this;
    }

    public String getCancelPath() {
        return cancelPath;
    }

    public void setCancelPath(String value) {
        this.cancelPath = value;
    }

    public EditorPage withCancelPath(String value) {
        setCancelPath(value);
        return this;
    }

    public EditorPage withCancelPath(String value, String defaultValue) {
        setCancelPath(value == null ? defaultValue : value);
        return this;
    }

    public List<EditorValue> getFields() {
        if (fields == null) {
            fields = Lists.newArrayList();
        }
        return fields;
    }

    public void setFields(List<EditorValue> value) {
        this.fields = value;
    }

    public EditorPage withFields(List<EditorValue> value) {
        setFields(value);
        return this;
    }

    public EditorPage withFields(List<EditorValue> value, List<EditorValue> defaultValue) {
        setFields(value == null ? defaultValue : value);
        return this;
    }

    public EditorPage addFields(EditorValue value) {
        if (fields == null) {
            fields = Lists.newArrayList();
        }
        this.fields.add(value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "name":
                return getName();
            case "savePath":
                return getSavePath();
            case "cancelPath":
                return getCancelPath();
            case "fields":
                return getFields();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof EditorPage)) return false;
        return signature().equals(((EditorPage) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean EditorPage has no signature defined, no hash code or equals possible.");
    }
}
